<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入姓名" suffix-icon="el-icon-search" v-model="resName"></el-input>
      <el-input style="width: 200px" placeholder="请输入楼号" suffix-icon="el-icon-message" class="ml-5" v-model="house"></el-input>
      <el-input style="width: 200px" placeholder="请输入单元号" suffix-icon="el-icon-position" class="ml-5" v-model="unit"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
<!--      <el-upload action="http://localhost:9090/api/user/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">-->
<!--        <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>-->
<!--      </el-upload>-->
      <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button>
    </div>

    <el-table :data="tableData.slice((pageNum-1)*pageSize,pageNum*pageSize)" style="width: 100%" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="resId" label="ID" width="50"></el-table-column>
      <el-table-column prop="resName" label="姓名" width="80"></el-table-column>
      <el-table-column prop="resSex" label="性别" width="80">
        <template slot-scope="scope">
          <p v-if="scope.row.resSex === 0">女</p>
          <p v-else>男</p>
        </template>
      </el-table-column>
      <el-table-column prop="resState" label="健康码状态">
        <template slot-scope="scope">
          <p v-if="scope.row.resState === 0">绿色</p>
          <p v-if="scope.row.resState === 1">黄色</p>
          <p v-if="scope.row.resState === 2">红色</p>
        </template>
      </el-table-column>
      <el-table-column prop="resPhone" label="电话"></el-table-column>
      <el-table-column prop="name" label="社区"></el-table-column>
      <el-table-column prop="house" label="楼号"></el-table-column>
      <el-table-column prop="unit" label="单元号"></el-table-column>
      <el-table-column prop="residentCount" label="住户人数"></el-table-column>
      <el-table-column prop="resIntotime" label="入住时间"></el-table-column>
      <el-table-column prop="resRemarks" label="备注"></el-table-column>
      <el-table-column prop="resState" label="风险等级">
        <template slot-scope="scope">
          <p v-if="scope.row.resState === 0">低风险</p>
        </template>
      </el-table-column>
      <el-table-column prop="resStatus" label="状态">
        <template slot-scope="scope">
          <p v-if="scope.row.resStatus === 0">正常</p>
          <p v-if="scope.row.resStatus === 1">已删除</p>
        </template>
      </el-table-column>
      <el-table-column label="操作"  width="200" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.resId)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="添加住户信息" :visible.sync="dialogFormVisible" width="30%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="姓名">
          <el-input v-model="form.resName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.resSex" label="1">男</el-radio>
          <el-radio v-model="form.resSex" label="0">女</el-radio>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.resPhone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
        <el-select v-model="form.resState" placeholder="请选择">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="备注信息">
          <el-input v-model="form.resRemarks" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="社区名称">
        <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="楼号">
          <el-input v-model="form.house" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单元号">
          <el-input v-model="form.unit" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>


    <el-dialog title="修改住户信息" :visible.sync="dialogForm" width="30%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="性别">
          <el-input v-model="form.resName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-radio v-model="form.resSex " label="1">男</el-radio>
          <el-radio v-model="form.resSex " label="0">女</el-radio>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.resPhone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.resState" placeholder="请选择">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息">
          <el-input v-model="form.resRemarks" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="社区名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="楼号">
          <el-input v-model="form.house" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单元号">
          <el-input v-model="form.unit" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>

export default {
  name: "ResidentList",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      resName: "",
      house: "",
      resSex: "",
      resRemarks: "",
      resPhone: "",
      resStatus: "",
      unit: "",
      resState: "",
      form: {},
      dialogFormVisible: false,
      dialogForm: false,
      multipleSelection: [],
      options: [{
        value: 0,
        label: '低风险'
      }, {
        value: 1,
        label: '中风险'
      }, {
        value: 2,
        label: '高风险'
      }],
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.request.get("/resident/getAll", {
        params: {
          resName: this.resName,
          house: this.house,
          unit: this.unit,
        }
      }).then(res => {
        console.log(res)
        //this.tableData = res.data
        this.total = res.total
        this.tableData = this.sortBykey(res.data, 'resId')
      })
    },
    sortBykey(ary, key) {
      return ary.sort(function (a, b) {
        let x = a[key]
        let y = b[key]
        return ((x < y) ? -1 : (x > y) ? 1 : 0)
      })
    },
    save() {
      this.request.post("/resident/addRes", this.form).then(res => {
        if (res.status == 201) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {
        // resSex: '1'
      }
      this.$set(this.form, 'resSex', '1')
      // this.form.resSex = "1"
    },
    edit(){
      this.request.post("/resident/edit", this.form).then(res => {
        if (res.status == 201) {
          this.$message.success("修改成功")
          this.dialogForm = false
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })

    },
    handleEdit(row) {
      console.log(row)
      this.form = row
      this.dialogForm = true
      if (row.resSex === 1){
        this.form.resSex = "1"
      }
      else {
        this.form.resSex = "0"
      }

    },
    del(id) {
      this.request.delete("/resident/upDelete/" + id).then(res => {
        if (res.status == 201) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.resId)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/resident/upDelete/batch", ids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    reset() {
      this.resName = ""
      this.house = ""
      this.unit = ""
      this.load()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum = 1;
      this.pageSize = val;
      this.load()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.load()
    },
    exp() {
      window.open("http://localhost:8088/resident/export")
    },
    // handleExcelImportSuccess() {
    //   this.$message.success("导入成功")
    //   this.load()
    // }
  }
}
</script>


<style>
.headerBg {
  background: #eee!important;
}
</style>